"use client"

import { ColumnDef } from "@tanstack/react-table"
import { Department } from "@/types/admin"
import { Button } from "@/components/ui/button"
import { ArrowUpDown, MoreHorizontal, Pencil, Trash2, Building2, Users } from "lucide-react"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import StatusBadge from "@/components/status-badge"

interface ColumnActionsProps {
  onEdit: (department: Department) => void
  onDelete: (department: Department) => void
}

export const columns = ({ onEdit, onDelete }: ColumnActionsProps): ColumnDef<Department>[] => [
  {
    accessorKey: "name",
    header: ({ column }) => {
      return (
        <Button
          variant="ghost"
          onClick={() => column.toggleSorting(column.getIsSorted() === "asc")}
          className="hover:bg-transparent"
        >
          部门名称
          <ArrowUpDown className="ml-2 h-4 w-4" />
        </Button>
      )
    },
    cell: ({ row }) => (
      <div className="font-medium flex items-center">
        <Building2 className="h-4 w-4 mr-2 text-primary" />
        {row.getValue("name")}
      </div>
    ),
  },
  {
    accessorKey: "parent",
    header: "上级部门",
    cell: ({ row }) => {
      const parent = row.getValue("parent") as Department | undefined;
      return parent ? (
        <div className="flex items-center">
          <Building2 className="h-4 w-4 mr-2 text-muted-foreground" />
          {parent.name}
        </div>
      ) : (
        <StatusBadge status="default">无上级部门</StatusBadge>
      );
    }
  },
  {
    accessorKey: "manager",
    header: "部门主管",
    cell: ({ row }) => {
      const manager = row.getValue("manager") as { name: string } | undefined;
      return manager ? (
        <div className="flex items-center">
          <Users className="h-4 w-4 mr-2 text-primary" />
          {manager.name}
        </div>
      ) : (
        <StatusBadge status="warning">未指定</StatusBadge>
      );
    }
  },
  {
    id: "actions",
    cell: ({ row }) => {
      const department = row.original

      return (
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button variant="ghost" className="h-8 w-8 p-0 rounded-full hover:bg-muted">
              <span className="sr-only">打开菜单</span>
              <MoreHorizontal className="h-4 w-4" />
            </Button>
          </DropdownMenuTrigger>
          <DropdownMenuContent align="end" className="rounded-lg card-shadow">
            <DropdownMenuLabel>操作</DropdownMenuLabel>
            <DropdownMenuItem 
              onClick={() => onEdit(department)}
              className="cursor-pointer flex items-center"
            >
              <Pencil className="mr-2 h-4 w-4" />
              编辑
            </DropdownMenuItem>
            <DropdownMenuItem
              className="text-destructive cursor-pointer flex items-center"
              onClick={() => onDelete(department)}
            >
              <Trash2 className="mr-2 h-4 w-4" />
              删除
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      )
    },
  },
] 